

#ifndef WIN32
#include <RTL.h>
#include <ctype.h>                    /* character functions                 */
#include <string.h>                   /* string and memory functions         */
#include <stdlib.h>
#include "File_Config.h"
#endif

#include <string.h>
#include <stdio.h>



#include "stm32f10x.h"
//#include "ucos_ii.h"


//#include "uart.h"

#define __GUIFS_DRV_C__
#include "GuiFs_Drv.h"

#define GUIFS_BUF_SIZE 100
u8 GuiFs_Buf[GUIFS_BUF_SIZE];
//HANDLE F_AudioFile;
FILE *F_AudioFile;
#define Not_Open_FILE NULL

void GuiFs_SetNodeDirName(u16 a)
{
	u8 i,t;
	memcpy(GuiFs_Buf,"M0:\\NODE    ",12);
	for(i = 0; i<4;i++)
	{
		t = (a>>12)&0x0f;
		if(t < 10)
			t += '0';
		else
			t += 'A'-10;
		GuiFs_Buf[i+7] = t;
		a <<= 4;
	}
	
}


u8 GuiFs_NewNodeDir(u8 *NodeDirName)
{
	#if 0
	char *Name;
	u8 Rt;
	if(NodeDirName == NULL)
		Name = (char *)GuiFs_Buf;
	else
	{
		Name =(char *) NodeDirName;
		memcpy(GuiFs_Buf,NodeDirName,20);
	}
	//Rt = OSMakeDir(Name);
	Rt = fopen(Name,"w");
	if(Rt == RETURN_OK)
		return 1;
	return 0;
	#else
	return 1;
	#endif
}

u8 GuiFs_NewAudioFile(u8 *DirName,u8 *FileName)
{
	u16 i;
	GuiFs_Buf[0] = 'M';
	for(i = 0;i<GUIFS_BUF_SIZE;i++)
		if(GuiFs_Buf[i] == 0)
			break;
	if(i<GUIFS_BUF_SIZE)
	{
		GuiFs_Buf[i++] = '\\';
		for(;i<GUIFS_BUF_SIZE;i++)
		{
			GuiFs_Buf[i] = *DirName++;
			if(*DirName == 0)
				break;
		}
		GuiFs_Buf[++i] = 0;
		GuiFs_Buf[i++] = '\\';
		for(;i<GUIFS_BUF_SIZE;i++)
		{
			GuiFs_Buf[i] = *FileName++;
			if(*FileName == 0)
				break;
		}
		GuiFs_Buf[++i] = 0;
		printf("Open File [%s]\r\n",GuiFs_Buf);
		#if 1
		F_AudioFile = fopen((char *)GuiFs_Buf, "wb");
		if(F_AudioFile != Not_Open_FILE)
			return 1;
		else return 0;
		#else
		return 1;
		#endif

	}
	return 0;
}

unsigned long  GuiFs_WriteAudioFile(u8 *buf,unsigned long l)
{
	u32 Rt = l;
	unsigned int i=0;
	unsigned int j=0;
	unsigned long data_length=0;
	unsigned char d_tmp[512];
	data_length =l;
	for(i=0;i<512;i++)
		d_tmp[i]=0;	   
	#if 1
	//Rt =  OSFileWrite(buf, l, F_AudioFile,0);
	while(data_length >512)
	{
		for(i=0;i<512;i++)
			d_tmp[i]= buf[i+512*j];
		Rt = fwrite(d_tmp,512,1,F_AudioFile);// fwrite(buf,1, l, F_AudioFile);
		data_length-=512;
		j++;
	}
	if(data_length<512){
		for(i=0;i<data_length;i++)
			d_tmp[i]=buf[i+512*j];
		Rt = fwrite(d_tmp,data_length,1,F_AudioFile);
	}
	else{
		for(i=0;i<512;i++)
			d_tmp[i]=buf[i+512*j];
		Rt = fwrite(d_tmp,512,1,F_AudioFile);
	}
	fclose(F_AudioFile);
	printf("Close File,Write %d / %d Bytes\r\n",Rt,l);
	#endif
	return Rt;
}

u8 GuiFs_EndAudioFile(void)
{
	#if 0
	if(F_AudioFile != Not_Open_FILE)
	{
		//OSFileClose2(&F_AudioFile);
		fclose(F_AudioFile);
		printf("Close File\r\n");
		return 1;
	}
	return 0;
	#else
	return 1;
	#endif
}

void GuiFs_Test(void)
{
	#if 0
	u32 i;
	GuiFs_SetNodeDirName(0x1ae1);
	GuiFs_NewNodeDir(NULL);
	GuiFs_NewAudioFile("20110315","180523.SPX");
	for(i = 0;i<sizeof(GuiFs_Buf);i++)
		GuiFs_Buf[i] = i;
	for(i = 0;i<1024;i++)
		GuiFs_WriteAudioFile(GuiFs_Buf,sizeof(GuiFs_Buf));
	
	GuiFs_EndAudioFile();
	#endif
}

static char *stringfind(char *s,char *f,u32 l,u32 ml)
{
	u32 i;
	for(i = 0;i<(ml-l);i++)
	{
		if(s[i] == 0)
			break;
		if(memcmp(s+i,f,l) == 0)
			break;
	}
	if((i<(ml-l))&&(s[i]!=0))
		return s+i;
	return NULL;
}

static char *stringufind(char *s,char *f,u32 l,u32 ml)
{
	u32 i;
	u32 j = 0;
	for(i = 0;i<(ml-l);i++)
	{
		if(s[i] == 0)
			break;
		if(memcmp(s+i,f,l)==0)
			j = i;
	}
	if(j != 0)
		return s+j;
	return NULL;
}

s8 Rt_DirSet(char *cd,char *fd,u32 fdl)
{
	s8 Rt = 0;
	char *p;
	char *p2;
	if((cd == NULL)||(fd[0] == 0))
		memcpy(fd,"*.*\0",4);
	else if(memcmp(cd,"..",2)==0)
	{
		p = stringfind(fd,"*.*\0",3,fdl);
		if(p != NULL)
		{
			*(p-1) = 0;
			p2 = stringufind(fd,"\\\0",1,fdl);
			if(p2 == NULL)
				*(p-1) = '\\';
			else
			{
				if(p2 != fd+2)
				{
					memcpy(p2+1,"*.*\0",4);
					Rt = -1;
				}
			}
		}
	}
	else
	{
		p = stringfind(fd,"*.*\0",3,fdl);
		if(p != NULL)
		{
			p2 = stringfind(cd,"]\0",1,20);
			if(p2 != NULL)
			{
				u8 i;
				*p2 = 0;
				i = strlen(cd+1);
				if(((p-fd)+i+5)<fdl)
				{
					memcpy(p,cd+1,i);
					memcpy(p+i,"\\*.*\0",5);
					Rt = 1;
				}
				*p2 = ']';
			}
		}	
	}
	return Rt;
}
extern  void cmd_dir (char *par);
u32 Rt_DirGet(char *buf,u32 step,u32 muns,char *dirname)
{
	u32 Rt = 0;
	char dir_cmd[10]="*.*";
	FINFO info;
	u32 l;
	char *p;
	#if 1
	init_card ();
	//fflush (stdout);
	// cmd_dir (dirname);
	 #if 1
	//OS_ENTER_CRITICAL();
	info.fileID  = 0;
	 while( (ffind (dirname,&info) == 0) &&(Rt <muns))
	 {
	 	p = buf+(step * Rt);
	 	l = strlen((char *)info.name);
	 	if (info.attrib & ATTR_DIRECTORY)
		{
			if((l+3)>step)
				l = step-1;
			*p = '[';
			memcpy(p+1,info.name,l);
			p[l+1] = ']';
			p[l+2] = 0;
		}
		else
		{
			if((l+1)>step)
				l = step-1;
			memcpy(p,info.name,l);
			p[l] = 0;
		}
	 	Rt++;
	 }
	 //OS_EXIT_CRITICAL();
	 #endif
	 #else
	 fs_main();
	 //fs_main_dir(dir_cmd);
	 #endif
	return Rt;
}




//end file

